package com.lwlk.zdk.controller.Role;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.pw.pwMenuMapper;
import com.lwlk.zdk.mapper.pw.pwPlatformRoleMapper;
import com.lwlk.zdk.mapper.pw.pwUserMapper;
import com.lwlk.zdk.model.*;
import com.lwlk.zdk.model.Encapsulation.EncapsulationMenu;
import com.lwlk.zdk.model.Encapsulation.EncapsulationRole;
import com.lwlk.zdk.model.pw.pwMenu;
import com.lwlk.zdk.model.pw.pwPlatformRole;
import com.lwlk.zdk.util.JwtTokenUtil;
import com.lwlk.zdk.util.LayuiTablePagingResult;
import com.lwlk.zdk.util.Response;
import com.lwlk.zdk.util.listerUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
    @RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    pwPlatformRoleMapper platformRoleMapper;
    @Autowired
    pwMenuMapper pwMenuMapper;
    @Autowired
    pwUserMapper pwUserMapper;


    @ApiOperation(value = "添加角色")
    @RequestMapping(value = "/roleAdd",method = RequestMethod.POST)
    public Response getRollAdd(@RequestBody pwPlatformRole pwPlatformRole,HttpServletRequest request){
        Response response=new Response();
        Boolean flag= listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
            int i=platformRoleMapper.insertSelective(pwPlatformRole);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }


    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "/roleUpdate",method = RequestMethod.POST)
    public Response getRoleUpdate(@RequestBody pwPlatformRole pwPlatformRole,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
            int i=platformRoleMapper.updateByPrimaryKeySelective(pwPlatformRole);

        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/deleteRole",method = RequestMethod.DELETE)
    public Response deleteRole(Integer id,Integer level,HttpServletRequest request){
        Response response=new Response();
        Boolean flag=listerUtil.getRoleType(request);
        if (flag==false){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此账号为监管员账号,不可进行操作");
            return response;
        }
        Boolean flage=listerUtil.getLevel(request);
        if (flage==false) {
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("该账号没有此权限");
            return response;
        }
        int sum=pwUserMapper.selectRole(id);
        if (sum>0){
            response.setCode(RetCode.REQUEST_ERROR);
            response.setMsg("此角色有绑定用户,不可删除");
            return response;
        }
        int i=platformRoleMapper.deleteByPrimaryKey(id);
        response.setData(i);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }

    @ApiOperation(value = "展示角色")
    @RequestMapping(value = "/showRole",method = RequestMethod.GET)
    public LayuiTablePagingResult geShowRole(pwPlatformRole pwPlatformRole,Integer page,Integer size){
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
            List<Map<String,Object>> platformRoleList=platformRoleMapper.selectByExample(pwPlatformRole);
            for (int i = 0; i < platformRoleList.size(); i++) {
                Map map=platformRoleList.get(i);
                String[] submenuId=String.valueOf(map.get("submenuId")).split(",");
                String SubmenuName=new String();
                if (map.get("submenuId")==null){
                    continue;
                }
                if (!map.get("submenuId").equals("")){
                    for (String id : submenuId) {
                        SubmenuName=SubmenuName+pwMenuMapper.selectSubmenuName(Integer.valueOf(id));
                    }
                }
            }
            PageInfo pageInfo = new PageInfo(platformRoleList);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
            Long totalPages = pageInfo.getTotal() / size;
            if (pageInfo.getTotal() % size != 0){
                totalPages ++;
            }
            result.setTotalPages(totalPages);
        }catch (Exception e){
            e.printStackTrace();
        }

       return result;
    }

    @ApiOperation(value = "展示角色不分页")
    @RequestMapping(value = "/showRoleNoPage",method = RequestMethod.GET)
    public Response showRoleNoPage(pwPlatformRole pwPlatformRole){
        Response response=new Response();
        List<Map<String,Object>> platformRoleList=platformRoleMapper.selectByExample(pwPlatformRole);
        response.setData(platformRoleList);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return  response;
    }

    @ApiOperation(value = "角色编号不可重复")
    @RequestMapping(value = "/numberRepetition",method = RequestMethod.GET)
    public Response getNumberRepetition(String roleCode){
        Response response=new Response();
        int count=platformRoleMapper.selectRoleCode(roleCode);
        if (count>0){
            response.setCode(RetCode.ACCOUNT_EXIST);
            response.setMsg("此编号已存在,请重新输入");
            return response;
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @ApiOperation(value = "展示角色详情")
    @RequestMapping(value = "/showRoleDetails",method = RequestMethod.GET)
    public Response getShowRoleDetails(Integer id){
        Response response=new Response();
        List<Map<String,Object>>roleDetail=platformRoleMapper.selectDetails(id);
        Map map=roleDetail.get(0);
        String[] menuIevelId= String.valueOf(map.get("menuIevelId")).split(",");//一级菜单
        String[] submenuId= String.valueOf(map.get("submenuId")).split(",");//二级菜单
        String[] threeLevelMenuId= String.valueOf(map.get("threeLevelMenuId")).split(",");//三级菜单
        ArrayList submenuList=new ArrayList();
        ArrayList threeLevelList=new ArrayList();
        ArrayList menuIevelList=new ArrayList();
        String submenuPath=new String();
        String submenuName=new String();
        String threeLevelMenuPath=new String();
        String threeLevelMenuName=new String();
        String menuIevelIdPath=new String();
        String menuIevelName=new String();
        for (String sId : submenuId) {
            Map map1=new HashMap();
            if (sId.equals("")||sId.equals("null")){
                continue;
            }
            submenuName=pwMenuMapper.selectSubmenuName(Integer.valueOf(sId));
            map1.put("submenuName",submenuName);
            submenuPath=pwMenuMapper.selectRoutingPath(Integer.valueOf(sId));
            map1.put("submenuPath",submenuPath);
            submenuList.add(map1);
        }
        for (String tId : threeLevelMenuId) {
            Map map2=new HashMap();
            if (tId.equals("")||tId.equals("null")){
                continue;
            }
            if (map.get("threeLevelMenuId").equals("")){
                map2.put("threeLevelMenuName",null);
                map2.put("threeLevelMenuPath",null);
            }else {
                threeLevelMenuName=pwMenuMapper.selectThreeLevelMenuName(Integer.valueOf(tId));
                map2.put("threeLevelMenuName",threeLevelMenuName);
                threeLevelMenuPath=pwMenuMapper.selectRoutingPath(Integer.valueOf(tId));
                map2.put("threeLevelMenuPath",threeLevelMenuPath);
                threeLevelList.add(map2);
            }
        }
        for (String mId : menuIevelId) {
            Map map3=new HashMap();
            if (mId.equals("")||mId.equals("null")){
                continue;
            }
            menuIevelName=pwMenuMapper.selectMenuIevelName(Integer.valueOf(mId));
            map3.put("menuIevelName",menuIevelName);
            menuIevelIdPath=pwMenuMapper.selectMenuIevelIdPath(Integer.valueOf(mId));
            map3.put("menuIevelIdPath",menuIevelIdPath);
            menuIevelList.add(map3);
        }
        map.put("submenu",submenuList);
        map.put("threeLevel",threeLevelList);
        map.put("menuIevelList",menuIevelList);
        response.setData(roleDetail);
        response.setCode(RetCode.SUCCESS);
        response.setMsg("执行成功");
        return response;
    }
}
